<?php
define('API_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/api.php');

testApi(
    'saveTemplate',
    array(
        'locale'        => 'en',
        'template'      => 'test',
        'description'   => 'Test description',
        'subject'       => 'Test subject',
        'body'          => 'Test body'
    )
);

testApi(
    'getTemplate',
    array(
        'locale'    => 'en',
        'template'  => 'test'
    )
);

testApi(
    'getTemplateList',
    array(
        'locale' => 'en'
    )
);

testApi(
    'deleteTemplate',
    array(
        'locale'    => 'en',
        'template'  => 'test'
    )
);

testApi(
    'sendImmediately',
    array(
        'locale'    => 'en',
        'template'  => 'account_activation',
        'email'     => 'anton.zarubin@gmail.com',
        'data'      => array(
            'first_name'        => 'Test',
            'last_name'         => 'Immediately',
            'activation_link'   => 'somelink',
        )
    )
);

testApi(
    'sendToQueue',
    array(
        'locale'    => 'en',
        'template'  => 'account_activation',
        'email'     => 'anton.zarubin@gmail.com',
        'data'      => array(
            'first_name'        => 'Test',
            'last_name'         => 'Queue',
            'activation_link'   => 'somelink',
        )
    )
);




function testApi($func, $params)
{
    $request = array();

    foreach ($params as $param => $value)
    {
        if (is_array($value))
        {
            foreach ($value as $k => $v)
            {
                $request[] = $param . '[' . $k . ']=' . urlencode($v);
            }
        }
        else
        {
            $request[] = $param . '=' . urlencode($value);
        }
    }

    $url = API_URL . '?do=' . $func . '&' . implode('&', $request);

    $data = file_get_contents($url);

    echo '<h3>' . $func . '</h3>';
    echo '<h4>' . $url . '</h4>';
    echo '<pre>';
    @print_r(unserialize($data));
    print_r($data);
    echo '</pre><br/>';
}